/**   
 * @Title: ActivitySupport.java 
 * @Package: com.rename.icode.activity 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-12 下午5:16:55 
 * @version  
 */

package com.rename.icode.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.rename.icode.R;
import com.rename.icode.dbg.LogcatHelper;
import com.rename.icode.utils.DateUtil;

/**
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-12 下午5:16:55
 * @version V1.0
 */

public class ActivitySupport extends Activity implements IActivitySupport {
	private static final String TAG = "ActivitySupport";

	protected Context context = null;
	 protected ICodeApplication icodeApplication;
	protected ProgressDialog pg = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		icodeApplication = (ICodeApplication) getApplication();
		pg = new ProgressDialog(context);
		pg.setCancelable(false);

		Log.d(this.toString(), "onCreate on " + DateUtil.getCurDateStr());

	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(this.toString(), "onStart on " + DateUtil.getCurDateStr());

	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(this.toString(), "onResume on " + DateUtil.getCurDateStr());

	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(this.toString(), "onPause on " + DateUtil.getCurDateStr());

	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(this.toString(), "onStop on " + DateUtil.getCurDateStr());
		hideProgress();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(this.toString(), "onDestroy on " + DateUtil.getCurDateStr());
		pg = null;
	}

	@Override
	public ProgressDialog getProgressDialog() {
		return pg;
	}

	public ProgressDialog showProgress(String title, String message) {
		return showProgress(title, message, -1);
	}

	public ProgressDialog showProgress(String title, String message, int theme) {
		if (pg == null) {
			if (theme > 0)
				pg = new ProgressDialog(this, theme);
			else
				pg = new ProgressDialog(this);
			pg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pg.requestWindowFeature(Window.FEATURE_NO_TITLE);
			pg.setCanceledOnTouchOutside(false);
			pg.setIndeterminate(true);
		}

		if (title != null)
			pg.setTitle(title);
		pg.setMessage(message);
		pg.show();
		return pg;
	}

	public void hideProgress() {
		if (pg != null) {
			pg.dismiss();
		}
	}

	@Override
	public void startConnService() {
		// TODO:开启连接服务器服务

	}

	@Override
	public void startGeneralService() {
		// TODO:开启一般服务

	}

	@Override
	public void stopService() {
		// TODO:停止所有服务
	}

	@Override
	public void isExit() {
		new AlertDialog.Builder(context)
				.setTitle(getResources().getString(R.string.settings_exit_btn))
				.setNeutralButton(getResources().getString(R.string.confirm),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								stopService();
								LogcatHelper.getInstance().stop();
								// eimApplication.exit();
							}
						})
				.setNegativeButton(getResources().getString(R.string.cancel),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						}).show();
	}

	@Override
	public boolean hasInternetConnected() {
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager != null) {
			NetworkInfo network = manager.getActiveNetworkInfo();
			if (network != null && network.isConnectedOrConnecting()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean hasLocationGPS() {
		LocationManager manager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if (manager
				.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean hasLocationNetWork() {
		LocationManager manager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if (manager
				.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean validateInternet() {
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager == null) {
			openWirelessSet();
			return false;
		} else {
			NetworkInfo[] info = manager.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		openWirelessSet();
		return false;
	}

	public void openWirelessSet() {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
		dialogBuilder
				.setTitle(R.string.prompt)
				.setMessage(context.getString(R.string.check_connection))
				.setPositiveButton(R.string.menu_settings,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
								Intent intent = new Intent(
										Settings.ACTION_WIRELESS_SETTINGS);
								context.startActivity(intent);
							}
						})
				.setNegativeButton(R.string.close,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int whichButton) {
								dialog.cancel();
							}
						});
		dialogBuilder.show();
	}

	@Override
	public void showToast(String text, int longint) {
		Toast.makeText(context, text, longint).show();
	}

	@Override
	public void showToast(String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	public void closeInput() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager != null && this.getCurrentFocus() != null) {
			inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	@Override
	public Context getContext() {
		return context;
	}

	@Override
	public String getVersion() {
		final String unknown = "Unknown";

		if (context == null) {
			return unknown;
		}

		try {
			String ret = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
			if (ret.contains(" + "))
				ret = ret.substring(0, ret.indexOf(" + ")) + "b";
			return ret;
		} catch (NameNotFoundException ex) {
		}

		return unknown;
	}

	public void changeLayout() {
	}

	public boolean isLoginActivity() {
		return false;
	}

	public boolean isSignUpActivity() {
		return false;
	}

	public void resetLoginBtn() {
	}

	/**
	 * hide login animation
	 */
	public void hideLoginAnim() {
	}

	/**
	 * Description 
	 * @return 
	 * @see com.rename.icode.activity.IActivitySupport#getICodeApplication() 
	 */ 
		
	@Override
	public ICodeApplication getICodeApplication() {
		return icodeApplication;
	}
}
